<?php
if (!defined('FosfoaSystem')) {
    exit(1);
}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
/**
 * NEWS新闻信息控制器
 * @author Fosf
 * @version 1.0
 * @created 2011-04-11
 */

class news extends general
{
    function __construct()
    { // 公用
        parent::__construct(); // 这是必须的
        $this->isnews = true;
    }

    /**
     * 信息分类
     */
    function index()
    {
        $this->isnewslist = true;

        $newsobj = spClass("newsModel");
        $newscategoriesobj = spClass("newscategoriesModel");


        $alias = urldecode($this->spArgs('alias', false));
        spClass('Safe')->StopAttack('alias',$alias);
        //空别名就返回所有新闻
        if (!$alias) {
            $request = $newscategoriesobj->FindformID(2); //通过ID获取根下的类信息
            $conditions = 'master_categories_id<>1'; //为空就返回信息分类下除公司介绍
        } else {
            //不为空，返回当前别名下的信息
            $request = $newscategoriesobj->FindformAlias($alias); //通过别名获取当前类信息
            if (!$request)
                ERROR404(1);
            $conditions = array('master_categories_id' => $request['categories_id']);
        }
        $title=$request['detail']['categories_title'];
        $keywords=$request['detail']['categories_keywords'];
        $description=$request['detail']['categories_meta_description'];
        $this->newscategory=$request;
        $this->newscategories_id = $request['categories_id'];
        $current_page = $this->spArgs("page", 1);
        $page_size = ALLNEWS;
        

        $request = $newsobj->spCache(3600 * 24)->News_list_for_conditions($conditions, $current_page,$page_size);
        
        $this->newslist = $request['newslist'];
        $this->pager = $request['pager'];

        $this->alias = $alias;
        
        $this->title = $title;
        $this->keywords = $keywords;
        $this->description = $description;
        $this->display("index/index_news_list.html");
    }
    /**
     * 信息搜索
     */
    function search()
    {
        $this->isnewssearch = true;
        $newsobj = spClass("news_descriptionModel");
        $newscategoriesobj = spClass("newscategoriesModel");

        $str = urldecode($this->spArgs('alias', false));
        spClass('Safe')->StopAttack('alias',$str);
        if (!$str)
            ERROR404(1);

        $str = Qkg($str); //将特殊字符转成空格
       
        $sqlstr = 'news_seokeywords like '. $newsobj->escape('%'.$str.'%') .' or news_description like '. $newsobj->escape('%'.$str.'%') .' or news_name like '. $newsobj->escape('%'.$str.'%');


        $conditions = $sqlstr; //主表单条件

        $current_page = $this->spArgs("page", 1);
        $page_size = ALLNEWS;

        $request = $newsobj->spCache(3600 * 24)->News_list_TAG_SEARCH($conditions, $current_page,
            $page_size);

        $this->newslist = $request['newslist'];
        $this->pager = $request['pager'];

        $this->alias = $str;
        $this->Fnav = 'SEARCH:' . $str;
        $this->title = "SEARCH:" . $str;
        $this->keywords = $str;
        $this->description = "SEARCH:" . $str;
        $this->display("index/index_news_search.html");
    }
    /**
     * TAG标签搜索
     */
    function tag()
    {
        $this->isnewstag = true;
        $newsobj = spClass("news_descriptionModel");
        $newscategoriesobj = spClass("newscategoriesModel");

        $str = urldecode($this->spArgs('alias', false));
        //spClass('Safe')->check($str);
        spClass('Safe')->StopAttack('alias',$str);
        if (!$str)
            ERROR404(1);

        $str = Qkg(urldecode($str)); //将特殊字符转成空格
        $sqlstr = 'news_seokeywords like '. $newsobj->escape('%'.$str.'%') .' or news_description like '. $newsobj->escape('%'.$str.'%');
        $conditions = $sqlstr; //主表单条件
        $current_page = $this->spArgs("page", 1);
        $page_size = ALLNEWS;

        $request = $newsobj->spCache(3600 * 24)->News_list_TAG_SEARCH($conditions, $current_page,
            $page_size);

        $this->newslist = $request['newslist'];
        $this->pager = $request['pager'];

        $this->alias = $str;
        $this->Fnav = 'TAG:' . $str;
        $this->title = "TAG:" . $str;
        $this->keywords = $str;
        $this->description = "TAG:" . $str;
        $this->display("index/index_news_tag.html");
    }

    function view()
    {
        $this->isnewsview = true;
        $alias = urldecode($this->spArgs('alias', false));
        spClass('Safe')->StopAttack('alias',$alias);
        if (!$alias)
            ERROR404(1);
        $news = spClass('newsModel')->FindformAlias($alias);
        if (!$news)
            ERROR404(1);
        spClass('newsModel')->hits($news['news_id']);//统计次数
        $this->news = $news;
        if (PL == '1')
            $this->Commentlist = spClass('news_commentsModel')->GetCommentsList($news['news_id']);
        $this->newscategories_id=$news['master_categories_id'];
        $this->alias = $alias;
        $this->title = $news['detail']['news_title'];
        $this->keywords = $news['detail']['news_seokeywords'];
        $this->description = $news['detail']['news_seodescription'];
        $this->display("index/index_news_view.html");
    }
    
    function about()
    {
        $this->isabout = true;
        $alias = urldecode($this->spArgs('alias', false));
        spClass('Safe')->StopAttack('alias',$alias);
        if (!$alias)
            ERROR404(1);
        $news = spClass('newsModel')->FindformAlias($alias);
        if (!$news)
            ERROR404(1);
            
        $this->news = $news;
        if (PL == '1')
            $this->Commentlist = spClass('news_commentsModel')->GetCommentsList($news['news_id']);
        $this->alias = $alias;
        $this->title = $news['detail']['news_title'];
        $this->keywords = $news['detail']['news_seokeywords'];
        $this->description = $news['detail']['news_seodescription'];
        $this->display("index/index_news_about.html");
    }
    //保存新闻信息评论
    function savecomment()
    {
        //dump($this->spArgs());exit;
        $news_id = $this->spArgs("commentnews_id");
        $action = $this->spArgs("action");
        $rating = $this->spArgs("rating", 5);
        $review_text = urldecode(strip_tags($this->spArgs("review_text")));
        spClass('Safe')->StopAttack('review_text',$review_text,2);
        $ip = getIP();

        if (isset($review_text) and isset($news_id)) {
            if (isset($this->userarr['customers_id'])) {
                $customers_id = $this->userarr['customers_id'];
            } else {
                $customers_id = 0;
            }
            if (PL == 0) {
                exit();
            } else {
                if (YKPL == 0 && $customers_id == 0) {
                    exit();
                }
            }


            $data = array(
                "news_id" => $news_id,
                "rating" => $rating,
                "review_text" => $review_text,
                "customers_id" => $customers_id,
                "ip" => $ip,
                "date_add" => time(),
                "type" => 0);
            $Obj = spClass('news_commentsModel');

            if ($action == 'process') {

                if ($Obj->SaveComment($data)) {
                    $msg = T("Reviews may require prior approval before they will be displayed!");
                    $status = 'y';
                } else {
                    $msg = T("Error comment failed!");
                    $status = 'n';
                }


            } else {
                $msg = T("Error comment failed!");
                $status = 'n';
            }
        } else {
            $msg = T("Error comment failed!");
            $status = 'n';
        }
        $result = array(
            'status' => $status,
            'info' => $msg,
            );
        echo json_encode($result); // 返回（显示）JSON结果
    }
}
